package com.hotel.front;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.GuestTypeLogic;

import data.GuestType;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class GuestTypeService extends HttpServlet{
	//http://localhost:8080/guesttypeService?func=addGuestType&title=1&description=aa
	//http://localhost:8080/guesttypeService?func=editGuestType&guesttypeId=870&title=12345&description=aabbb
	//http://localhost:8080/guesttypeService?func=delGuestType&guesttypeId=870
	//http://localhost:8080/guesttypeService?func=changeGuestType&guesttypeId=870
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addGuestType")){
				AddGuestType(req,resp);
			}else if(doClass.equals("editGuestType")){
				EditGuestType(req,resp);
			}else if(doClass.equals("delGuestType")){
				DeleteGuestType(req,resp);
			}else if(doClass.equals("changeGuestType")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("getAllGuestType")){
				GetAllGuestType(req,resp);
			}else if(doClass.equals("getGuestTypeById")){
				GetGuestTypeById(req,resp);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void AddGuestType(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		String title = req.getParameter("title");
		String description = req.getParameter("description");

		Object obj = new GuestTypeLogic().AddGuestType(hotelId,title,description);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}

	private void EditGuestType(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		long guesttypeId = 0;
		if(!req.getParameter("guesttypeId").equals("")){
			guesttypeId = Long.parseLong(req.getParameter("guesttypeId"));
		}
		
		String title = req.getParameter("title");
		String description = req.getParameter("description");
		
		if(guesttypeId == 0){
			new GuestTypeLogic().AddGuestType(hotelId,title,description);
		}else{
			new GuestTypeLogic().EditGuestType(guesttypeId,title,description);
		}
		 
		GetAllGuestType(req, resp);

	}

	private void DeleteGuestType(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String guesttypeId = req.getParameter("id");
		for(int i = 0; i < guesttypeId.split(",").length ; i++){
			new GuestTypeLogic().DeleteGuestType(Long.parseLong(guesttypeId.split(",")[i]));
		}

		GetAllGuestType(req, resp);
	}
	
	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long guesttypeId = Long.parseLong(req.getParameter("id"));

		Object obj = new GuestTypeLogic().ChangeStatus(guesttypeId);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}	
	
	private void GetAllGuestType(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		Object obj = new GuestTypeLogic().GetAllGuestType4AdminLimit(hotelId,offset,line);
		double dataSize = new GuestTypeLogic().GetAllGuestType4Admin(hotelId).size();

		Gson gson = new Gson();
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetGuestTypeById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		GuestType obj = new GuestTypeLogic().GetGuestType(id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
}
